package com.foreveross.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
 
import com.foreveross.service.LogService;
import com.foreveross.service.TenementCostService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.ExcelUtil;
import com.foreveross.vo.OrderInfo;
import com.foreveross.vo.TenementCost;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysTenementCostController")
public class SysTenementCostController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysTenementCostController.class);

	@Resource
	private TenementCostService tenementCostService;

	@Resource
	private LogService logService;
	
	
	/**
	 * 查看缴费记录
	 * 
	 * @param request
	 * @param response
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getTenementCostList")
	public void getTenementCostList(HttpServletRequest request,
			HttpServletResponse response, String rows,
			String page,String weixinID,String userId, String houseId,String resCode,
			String IpItemName, String status,String type) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) tenementCostService.getTenementCostListCount(weixinID,userId,houseId,
					 IpItemName,status,resCode,type);
			PageVo<TenementCost> pageObj = new PageVo<TenementCost>(page, countRecord,rows);
			pageObj.setRows(tenementCostService.findTenementCostList(weixinID,userId,houseId,
					 IpItemName,status,resCode,type, pageObj.getpageIndex(),pageObj.getpageSize()));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("缴费记录 查看失败");
			log.error("缴费记录 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/exportTenementCostListExcel")
	public void exportTenementCostListExcel(HttpServletRequest request,
			HttpServletResponse response,String weixinID,String userId, String houseId,
			String IpItemName, String status,String resCode,String type) {
		try {
			List<TenementCost> list = tenementCostService.findTenementCostList(weixinID,userId,houseId,
					 IpItemName,status,resCode,type, 0,Constants.PAGE_MAX_SIZE);
			String fileName= "";
			if(type.equals(Constants.FIND_COST_CAR)) fileName="停车缴费管理";
			else if(type.equals(Constants.FIND_COST_TENEMENT)) fileName="物业缴费管理";
			String [] titles ={
					"流水号", "楼盘", "姓名","房号", "费用类型", "金额(元)", 
					"时间", "状态"
			};
			String [] valueName ={
					"billNo", "hrHouses-name", "houseUser-name", "houseUser-resCode","ipItemName", "cost", 
					"chargeTime", "statusName"
			};
			ExcelUtil<TenementCost> excelUtil = new ExcelUtil<TenementCost>(fileName, titles,valueName, list);
			excelUtil.expExcel(response);
			log.info("缴费记录 EXCEL 导出成功!");
		} catch (Exception e) {
			log.error("缴费记录 EXCEL 导出失败!", e);
		}
	}
	
	/**
	 * 查看缴费详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getTenementCost")
	public void getTenementCost(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			TenementCost hrHouses = (TenementCost) this.findObject(
					TenementCost.class, id);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看缴费账单信息失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 缴费记录添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
/*	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateTenementCost")
	public void addOrUpdateTenementCost(HttpServletRequest request,
			HttpServletResponse response, TenementCost tenementCost,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			tenementCostService.saveOrUpdate(tenementCost, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"缴费记录" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "缴费记录["
							+ tenementCost.toString() + "]");
			log.info("缴费记录 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("缴费记录 " + updateType + "失败");
			log.error("缴费记录" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}*/

	/**
	 * 缴费记录删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
/*	@RequestMapping(method = RequestMethod.POST, value = "/deleteTenementCostList")
	public void deleteTenementCostList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			tenementCostService.deleteObjecList("TenementCost", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "缴费记录删除",
					AuthUtil.getUserid(request), "缴费记录[" + ids + "]");
			log.info("缴费记录 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("缴费记录 删除失败");
			log.info("缴费记录 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}*/

}
